package com.volcreate.exception_collection.no9;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author zxc
 * @date 2020/10/6
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String s = scanner.next();
            if ("quit".equals(s)) {
                break;
            } else {
                ArrayListGeneralStack stack = new ArrayListGeneralStack();
                int m = scanner.nextInt();
                int n = scanner.nextInt();

                System.out.println(s + " Test");
                if ("Integer".equals(s)) {
                    while (m-- > 0) {
                        int i = scanner.nextInt();
                        System.out.println("push:" + stack.push(i));
                    }
                    while (n-- > 0) {
                        System.out.println("pop:" + stack.pop());
                    }
                    System.out.println(stack);
                    int sum = 0;
                    while (!stack.empty()) {
                        sum += (int) stack.pop();
                    }
                    System.out.println("sum=" + sum);
                    System.out.println(stack.getClass().getInterfaces()[0]);
                }
                if ("Double".equals(s)) {
                    while (m-- > 0) {
                        double i = scanner.nextDouble();
                        System.out.println("push:" + stack.push(i));
                    }
                    while (n-- > 0) {
                        System.out.println("pop:" + stack.pop());
                    }
                    System.out.println(stack);
                    double sum = 0;
                    while (!stack.empty()) {
                        sum += (double) stack.pop();
                    }
                    System.out.println("sum=" + sum);
                    System.out.println(stack.getClass().getInterfaces()[0]);
                }
                if ("Car".equals(s)) {
                    while (m-- > 0) {
                        Car car = new Car();
                        car.setId(scanner.nextInt());
                        car.setName(scanner.next());
                        System.out.println("push:" + stack.push(car));
                    }
                    while (n-- > 0) {
                        System.out.println("pop:" + stack.pop());
                    }
                    System.out.println(stack);
                    while (!stack.empty()) {
                        Car car = (Car) stack.pop();
                        System.out.println(car.getName());
                    }
                    System.out.println(stack.getClass().getInterfaces()[0]);
                }
            }

        }

    }
}

interface GeneralStack<E> {
    public E push(E item); //如item为null，则不入栈直接返回null。否则直接入栈，然后返回item。

    public E pop();              //出栈，如栈为空，则返回null。

    public E peek();             //获得栈顶元素，如栈顶为空，则返回null。注意：不要出栈

    public boolean empty();           //如过栈为空返回true

    public int size();                //返回栈中元素数量
}

class ArrayListGeneralStack<E> implements GeneralStack<E> {

    private ArrayList<E> list = list = new ArrayList<>();
    ;

    public ArrayListGeneralStack() {
    }

    @Override
    public E push(E item) {
        if (item == null) return null;
        list.add(item);
        return item;
    }

    @Override
    public E pop() {
        if (list.isEmpty()) return null;
        return list.remove(list.size() - 1);
    }

    @Override
    public E peek() {
        if (list.isEmpty()) {
            return null;
        }
        return list.get(list.size() - 1);
    }

    @Override
    public boolean empty() {
        return list.isEmpty();
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public String toString() {
        return list.toString();
    }
}

class Car {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [" +
                "id=" + id +
                ", name=" + name +
                ']';
    }
}